var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var WxParse = require('../../wxParse/wxParse.js');
var app = getApp();
var hasMore = true;
var goinMore = true;
var page = 1;
Page({

  /**
     * 页面的初始数据
     */
  data: {
    uid: 0,
    token: 0,
    openid: 0,
    userinfo: {},
    weiguanUserData: [],

    hongbaoId: '',//红包id
    shareuid: '', //分享者id

    hasMingPian: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    hasMore = true;
    goinMore = true;
    page = 1;
    
    var that = this;
    //红包id
    this.data.hongbaoId = options.hongbaoid;
    //分享者id
    this.data.shareuid = options.shareuid;

    app.LoginUserInfo(function (res) {
      if (res && res != -1) {
      var cata = (parseInt(res.usercata) > 0) ? true : false;
      that.setData({
        uid: res.uid,
        token: res.token,
        openid: res.wxsmall_openid,
        hasMingPian: cata
      });
      }
    });

    app.GetUserInfo(that.data.uid, function (res) {
      that.setData({
        userinfo: res
      });
    });

    this.getHongbaoViewlist(1);
  },

  goAtherPage: function (e) {
    var url = e.currentTarget.dataset.link;
    if (url) {
      wx.navigateTo({ url: url })
    }
  },

  loadMore: function () {
    if (hasMore && goinMore) {
      goinMore = false;
      page++;
      this.getHongbaoViewlist(page);
    } else {
      console.log("没有更多了");
    }
  },

  //获取围观用户列表
  getHongbaoViewlist: function (cpage) {
    var that = this;
    util.api({
      method: "GET",
      data: {
        ac: "hongbaoviewlist",
        hongbaoid: that.data.hongbaoId,
        shareuid: that.data.shareuid,
        page: cpage,
        num: 20,
        publickey: util.publiccode
      },
      success: function (responseData) {
        console.log("hongbaoviewlist ", responseData);
        if (responseData && responseData.statusCode == 200) {
          var result = responseData.data;
          if (result && result.length > 0) {
            for (var i = 0; i < result.length; i++) {
              var date = new Date(result[i].datetime * 1000);
              result[i].formatTime = util.formatTime(date, '-', ':');
            }
            
            that.setData({
              weiguanUserData: that.data.weiguanUserData.concat(result),
            });
          } else {
            hasMore = false;
          }
          goinMore = true;
        }

      }
    });
  },  

})